import java.awt.Canvas;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;

import javax.swing.Scrollable;
import javax.swing.SwingConstants;


public class ImageMergeCanvas extends Canvas implements Scrollable
{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private static BufferedImage mPreviewImage = null;
	private int maxUnitIncrement = 1;
	
	ImageMergeCanvas(BufferedImage img)
	{
		mPreviewImage = img;
	}

	public void paint(Graphics g)
	{
		super.paint(g);
		if(mPreviewImage != null)
		{
			g.drawImage(mPreviewImage, 0, 0, null);
		}
	}
	
	public void UpdateImage(BufferedImage img)
	{
		if(img != null)
		{
			mPreviewImage = img;
		}
		repaint();
	}

	@Override
	public Dimension getPreferredScrollableViewportSize()
	{
		return getPreferredSize();
	}

	@Override
	public int getScrollableBlockIncrement(Rectangle visibleRect,
			int orientation, int direction)
	{
		if (orientation == SwingConstants.HORIZONTAL) {
            return visibleRect.width - maxUnitIncrement;
        } else {
            return visibleRect.height - maxUnitIncrement;
        }

	}

	@Override
	public boolean getScrollableTracksViewportHeight()
	{
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean getScrollableTracksViewportWidth()
	{
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public int getScrollableUnitIncrement(Rectangle visibleRect,
			int orientation, int direction)
	{
		//Get the current position.
        int currentPosition = 0;
        if (orientation == SwingConstants.HORIZONTAL) {
            currentPosition = visibleRect.x;
        } else {
            currentPosition = visibleRect.y;
        }

        //Return the number of pixels between currentPosition
        //and the nearest tick mark in the indicated direction.
        if (direction < 0) {
            int newPosition = currentPosition -
                             (currentPosition / maxUnitIncrement)
                              * maxUnitIncrement;
            return (newPosition == 0) ? maxUnitIncrement : newPosition;
        } else {
            return ((currentPosition / maxUnitIncrement) + 1)
                   * maxUnitIncrement
                   - currentPosition;
        }

	}
}
